package com.travel.controller.user;

import com.travel.common.UserContext;
import com.travel.entity.dto.TravelPlanDTO;
import com.travel.entity.po.TravelPlan;
import com.travel.entity.vo.TravelPlanVo;
import com.travel.result.Result;
import com.travel.service.TravelPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:Jasenon_ce
 * @Date: 2025/03/16/下午5:09
 */
@RequestMapping("/user/plan")
@RestController
@Slf4j
@Api(tags = "用户旅行计划")
public class UserTravelPlanController {

    @Autowired
    private TravelPlanService travelPlanService;

    @ApiOperation("生成用户旅行路线")
    @PostMapping("/create")
    public Result<?> create(@Validated @RequestBody TravelPlanDTO travelPlanDTO, HttpServletRequest request){
        log.info("开始创建旅行计划: {}", travelPlanDTO.getTitle());
        
        // 检查用户是否已登录
        if (UserContext.getUserId() == null) {
            log.warn("未登录用户尝试创建旅行计划");
            return Result.fail(401, "请先登录");
        }
        
        try {
            travelPlanService.createTravelPlan(travelPlanDTO, request);
            log.info("旅行计划创建任务已提交: {}", travelPlanDTO.getTitle());
            return Result.success("旅行计划生成任务已提交，请稍后查看结果");
        } catch (Exception e) {
            log.error("创建旅行计划失败", e);
            return Result.error("创建旅行计划失败: " + e.getMessage());
        }
    }
    
    @ApiOperation("获取当前用户的所有旅行计划")
    @GetMapping("/list")
    public Result<List<TravelPlanVo>> listUserPlans() {
        log.info("获取用户旅行计划列表");
        
        // 检查用户是否已登录
        if (UserContext.getUserId() == null) {
            log.warn("未登录用户尝试获取旅行计划列表");
            return Result.fail(401, "请先登录");
        }
        
        try {
            List<TravelPlanVo> plans = travelPlanService.getUserTravelPlans();
            return Result.success(plans);
        } catch (Exception e) {
            log.error("获取旅行计划列表失败", e);
            return Result.error("获取旅行计划列表失败: " + e.getMessage());
        }
    }
    
    @ApiOperation("获取特定旅行计划详情")
    @GetMapping("/{planId}")
    public Result<TravelPlan> getPlanDetail(
            @ApiParam(value = "旅行计划ID", required = true) 
            @PathVariable Long planId) {
        log.info("获取旅行计划详情, ID: {}", planId);
        
        // 检查用户是否已登录
        if (UserContext.getUserId() == null) {
            log.warn("未登录用户尝试获取旅行计划详情");
            return Result.fail(401, "请先登录");
        }
        
        try {
            TravelPlan plan = travelPlanService.getTravelPlanById(planId);
            return Result.success(plan);
        } catch (Exception e) {
            log.error("获取旅行计划详情失败", e);
            return Result.error("获取旅行计划详情失败: " + e.getMessage());
        }
    }
}
